package cn.com.huzo.webapp.controller.tech;

import cn.com.huzo.model.entities.tech.PieceYardageCatalogueEntity;
import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.service.tech.PieceYardageCatalogueManager;
import huzofw.model.entities.viewmodel.TreeNode;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 单耗目录控制器
 *
 * @author 吴旭
 */
@Controller
public class PieceYardageCatalogueController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "PieceYardageCatalogueManager")
    private PieceYardageCatalogueManager _manager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分"> 
    /**
     * 跳转单耗目录编辑画面
     * <p>
     * @return
     */
    @RequestMapping("tech/piece_yardage/catalogue/edit.html")
    public ModelAndView redirectEditCatalogueWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("tech.piece_yardage_catalogue.edit.def");
        return view;
    }

    /**
     * 弹出单耗目录选择画面
     * <p>
     * @return
     */
    @RequestMapping("tech/piece_yardage/catalogue/choice.html")
    public ModelAndView redirectCatalogueChoice() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.tech.piece_yardage_catalogue.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="单耗目录处理部分">
    /**
     * 取得目录数节点数据。
     *
     * @param parentKey 所属目录标识
     * @return 该目录下的子部们数据节点列表。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/piece_yardage/catalogue/tree.html",
            method = RequestMethod.GET)
    @ResponseBody
    public List<TreeNode> getCatalogueTreeNode(@RequestParam Long parentKey) throws Exception {
        List<TreeNode> _listProductCatalogueTree = _manager.fetchTreeNodeByParentKey(parentKey);
        return _listProductCatalogueTree;
    }

    /**
     * 变更所属父目录信息。
     * <p>
     * @param criterion 条件
     * @return 数据
     * @throws Exception
     */
    @RequestMapping(value = "tech/piece_yardage/catalogue/changeParentCatalogue.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer changeParentCatalogue(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.changeParentCatalogue(criterion);
    }

    /**
     * 取得当前目录数据。
     *
     * @param key 当前目录ID
     * @return 当前目录数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/piece_yardage/catalogue/fetchByKey.html",
            method = RequestMethod.GET)
    public @ResponseBody
    PieceYardageCatalogueEntity fetchCatalogueByKey(@RequestParam Long key) throws Exception {
        return _manager.fetchByKey(key);
    }

    /**
     * 删除单耗目录信息数据。
     *
     * @param instance 目录标识
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/piece_yardage/catalogue/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody PieceYardageCatalogueEntity instance) throws Exception {
        return _manager.delete(instance);
    }

    /**
     * 保存目录数据。
     * <p>
     * @param instance 要保存的信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "tech/piece_yardage/catalogue/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody PieceYardageCatalogueEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getCatalogueId() > 0) { //编辑
            rowsAffected = (long) _manager.update(instance);
        } else {//新增
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }
    // </editor-fold>
}
